package com.lean.android.projecttemplate.ui.main

import android.widget.Toast
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.airbnb.mvrx.compose.collectAsState
import com.airbnb.mvrx.compose.mavericksViewModel
import com.lean.android.projecttemplate.appModule
import com.lean.android.projecttemplate.ui.AppState
import com.lean.android.projecttemplate.ui.TopLevelDestination
import com.lean.android.projecttemplate.ui.newpage.newPageScreen
import com.lean.android.projecttemplate.ui.rememberAppState
import com.lean.android.projecttemplate.ui.speed.speedScreen
import org.koin.compose.KoinApplication
import org.koin.compose.koinInject
import org.koin.compose.viewmodel.koinViewModel

@Preview
@Composable
fun PreviewMainNav() {
    KoinApplication(application = {
        modules(appModule)
    }) {
        App()
    }
}

@Composable
fun App(navController: NavHostController = rememberNavController()) {
    val appState = rememberAppState(navController)

    Scaffold(bottomBar = {
        if (appState.shouldShowBottomBar) {//control bottom bar.
            val context = LocalContext.current
            BottomTab(appState, onItemClick = { destination ->
                when (destination) {
                    TopLevelDestination.Main, TopLevelDestination.Speed -> {
                        appState.navigateToTopDestination(destination)
                    }

                    else -> {
                        Toast.makeText(context, destination.titleTextId, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }

    }) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = TopLevelDestination.Main.route,
            modifier = Modifier.padding(innerPadding),
            enterTransition = {
                fadeIn()
            },
            exitTransition = {
                fadeOut()
            }) {
            mainScreen()
            speedScreen(navController)
            newPageScreen(navController)
        }


    }

}

@Composable
fun BottomTab(appState: AppState, onItemClick: (TopLevelDestination) -> Unit) {
    NavigationBar(
        tonalElevation = 1.dp, containerColor = MaterialTheme.colorScheme.surface
    ) {
        TopLevelDestination.entries.forEachIndexed { _, topLevelDestination ->
            val selected = topLevelDestination.route == appState.currentRouteState
            NavigationBarItem(selected = selected, onClick = {
                onItemClick(topLevelDestination)
            }, icon = {
                Icon(
                    if (selected) topLevelDestination.selectedIcon else topLevelDestination.unselectedIcon,
                    contentDescription = stringResource(id = topLevelDestination.titleTextId)
                )
            }, label = {
                Text(text = stringResource(id = topLevelDestination.titleTextId))
            })
        }
    }

}

@Composable
fun MainScreen() {
    val viewModel: CountViewModel = mavericksViewModel()
    val count by viewModel.collectAsState(CountState::count)
    Greeting(name = "MainPage", count = count, onButtonClick = {
        viewModel.incrementCount()
    })
}

@Composable
fun Greeting(
    name: String, count: Int, modifier: Modifier = Modifier, onButtonClick: () -> Unit = {}
) {
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Hello $name, you click $count times.",
            fontSize = 32.sp,
            fontWeight = FontWeight.Bold,
            lineHeight = TextUnit(1.2f, TextUnitType.Em),
            modifier = modifier.padding(horizontal = 40.dp)
        )

        Spacer(modifier = Modifier.height(20.dp))

        Button(
            onClick = {
                onButtonClick()
            }, modifier = Modifier
        ) {
            Text(
                text = "Increase", fontSize = 20.sp
            )
        }
    }
}
